cmake_minimum_required(VERSION 3.14)
if (ENABLE_OPEN_SRC)
    find_package(c_base CONFIG REQUIRED)
    find_package(c_mmpa CONFIG REQUIRED)
endif()

set(INSTALL_LIBRARY_DIR lib)
set(SRC_LIST
    common/acl.c
    runtime/callback.c
    runtime/context.c
    runtime/stream.c
    runtime/device.c
    runtime/memory.c
    runtime/host_func.c
    model/model_config.c
    model/model_exec_config.c
    model/model.c
    model/model_desc.c
    types/tensor_desc_internal.c
)

if (${TARGET_SYSTEM_NAME} STREQUAL "Linux")
    # Linux start
    set(ASCENDCL_C_COMPILE_TARGET ascendcl_c)
    add_library(${ASCENDCL_C_COMPILE_TARGET} SHARED ${SRC_LIST})
    target_include_directories(ascendcl_c PRIVATE
    if(NOT ENABLE_OPEN_SRC)
        ${TOP_DIR}/inc
        ${TOP_DIR}/inc/external
    else()
        $<$<BOOL:${ENABLE_OPEN_SRC}>:${ASCEND_OPENSDK_DIR}/include/>
        $<$<BOOL:${ENABLE_OPEN_SRC}>:${ASCEND_OPENSDK_DIR}/include/air>
        $<$<BOOL:${ENABLE_OPEN_SRC}>:${ASCEND_OPENSDK_DIR}/include/runtime>
        $<$<BOOL:${ENABLE_OPEN_SRC}>:${ASCEND_OPENSDK_DIR}/include/msprof>
        $<$<BOOL:${ENABLE_OPEN_SRC}>:${ASCEND_OPENSDK_DIR}/include/slog>
    endif()
        ${BASE_DIR}/c/
        ${BASE_DIR}/c/common/
        ${BASE_DIR}/inc/external
        ${BASE_DIR}/inc/external/acl
        ${BASE_DIR}/inc
    )

    target_compile_options(${ASCENDCL_C_COMPILE_TARGET} PRIVATE
        $<$<STREQUAL:${TARGET_SYSTEM_NAME},Linux>:-fvisibility=hidden -O2 -fno-common>
        $<$<STREQUAL:${TARGET_SYSTEM_NAME},Windows>:/utf-8 /Od>
        $<$<AND:$<STREQUAL:${TARGET_SYSTEM_NAME},Windows>,$<STREQUAL:${CMAKE_CONFIGURATION_TYPES},Debug>>:/MTd>
        $<$<AND:$<STREQUAL:${TARGET_SYSTEM_NAME},Windows>,$<STREQUAL:${CMAKE_CONFIGURATION_TYPES},Release>>:/MT>
    )

    target_compile_definitions(${ASCENDCL_C_COMPILE_TARGET} PRIVATE
        FUNC_VISIBILITY
        $<IF:$<STREQUAL:${TARGET_SYSTEM_NAME},LiteOS>,NANO_OS_TYPE=1,NANO_OS_TYPE=0>
    )

    target_link_libraries(${ASCENDCL_C_COMPILE_TARGET} PRIVATE
        $<BUILD_INTERFACE:intf_pub>
        $<$<NOT:$<BOOL:${ENABLE_OPEN_SRC}>>:$<BUILD_INTERFACE:msprof_headers>>
        c_sec_headers
        -Wl,--no-as-needed
        ge_executor_c_static
        -Wl,--as-needed
        slog_headers
        -s
        c_base_headers
        c_mmpa_headers
    )

    target_link_options(${ASCENDCL_C_COMPILE_TARGET} PRIVATE
        -Wl,-Bsymbolic
    )

    set_target_properties(${ASCENDCL_C_COMPILE_TARGET} PROPERTIES
        OUTPUT_NAME ascendcl
    )

    install(TARGETS ${ASCENDCL_C_COMPILE_TARGET} OPTIONAL
        LIBRARY DESTINATION ${INSTALL_LIBRARY_DIR}/c
    )
    # Linux end
elseif (${TARGET_SYSTEM_NAME} STREQUAL "LiteOS")
    # LiteOS start
    set(ASCENDCL_C_COMPILE_TARGET ascendcl_c_static_liteos)
    add_library(${ASCENDCL_C_COMPILE_TARGET} STATIC ${SRC_LIST})
    target_include_directories(${ASCENDCL_C_COMPILE_TARGET} PRIVATE
    if(NOT ENABLE_OPEN_SRC)
        ${TOP_DIR}/inc
        ${TOP_DIR}/inc/external
    else()
        $<$<BOOL:${ENABLE_OPEN_SRC}>:${ASCEND_OPENSDK_DIR}/include/>
        $<$<BOOL:${ENABLE_OPEN_SRC}>:${ASCEND_OPENSDK_DIR}/include/air>
        $<$<BOOL:${ENABLE_OPEN_SRC}>:${ASCEND_OPENSDK_DIR}/include/runtime>
        $<$<BOOL:${ENABLE_OPEN_SRC}>:${ASCEND_OPENSDK_DIR}/include/msprof>
        $<$<BOOL:${ENABLE_OPEN_SRC}>:${ASCEND_OPENSDK_DIR}/include/slog>
    endif()
        ${BASE_DIR}/c/
        ${BASE_DIR}/c/common/
        ${BASE_DIR}/inc/external
        ${BASE_DIR}/inc/external/acl
        ${BASE_DIR}/inc
    )

    target_compile_options(${ASCENDCL_C_COMPILE_TARGET} PRIVATE
        $<$<STREQUAL:${TARGET_SYSTEM_NAME},Linux>:-fvisibility=hidden -O2 -fno-common>
        $<$<STREQUAL:${TARGET_SYSTEM_NAME},Windows>:/utf-8 /Od>
        $<$<AND:$<STREQUAL:${TARGET_SYSTEM_NAME},Windows>,$<STREQUAL:${CMAKE_CONFIGURATION_TYPES},Debug>>:/MTd>
        $<$<AND:$<STREQUAL:${TARGET_SYSTEM_NAME},Windows>,$<STREQUAL:${CMAKE_CONFIGURATION_TYPES},Release>>:/MT>
    )

    target_compile_definitions(${ASCENDCL_C_COMPILE_TARGET} PRIVATE
        FUNC_VISIBILITY
        $<IF:$<STREQUAL:${TARGET_SYSTEM_NAME},LiteOS>,NANO_OS_TYPE=1,NANO_OS_TYPE=0>
        LITEOS_PRINT
    )

    target_link_libraries(${ASCENDCL_C_COMPILE_TARGET} PRIVATE
        $<BUILD_INTERFACE:intf_pub>
        $<$<NOT:$<BOOL:${ENABLE_OPEN_SRC}>>:$<BUILD_INTERFACE:msprof_headers>>
        c_sec_headers
        -Wl,--no-as-needed
        ge_executor_c_static
        -Wl,--as-needed
        slog_headers
        -s
        c_base_headers
        c_mmpa_headers
    )

    target_link_options(${ASCENDCL_C_COMPILE_TARGET} PRIVATE
        -Wl,-Bsymbolic
    )

    set_target_properties(${ASCENDCL_C_COMPILE_TARGET} PROPERTIES
        OUTPUT_NAME ascendcl_liteos
    )

    install(TARGETS ${ASCENDCL_C_COMPILE_TARGET} OPTIONAL
        ARCHIVE DESTINATION ${INSTALL_LIBRARY_DIR}/c
    )

    # LiteOS打包
    set(ASCENDCL_C_STATIC_LIB_NAME "libascendcl.a")
    if(NOT ENABLE_OPEN_SRC)
        file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/ascendcl_c_static.mri
        "create ${CMAKE_CURRENT_BINARY_DIR}/${ASCENDCL_C_STATIC_LIB_NAME}
            addlib ${CMAKE_CURRENT_BINARY_DIR}/libascendcl_liteos.a
            addlib ${CMAKE_CURRENT_BINARY_DIR}/../../../../air/executor_c/executor/libge_executor.a
            addlib ${CMAKE_CURRENT_BINARY_DIR}/../../runtime/c/feature/libruntime.a
            addlib ${CMAKE_CURRENT_BINARY_DIR}/../../runtime/c/c_base/libc_base.a
            save
            end")
    else()
        file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/ascendcl_c_static.mri
        "create ${CMAKE_CURRENT_BINARY_DIR}/${ASCENDCL_C_STATIC_LIB_NAME}
            addlib ${CMAKE_CURRENT_BINARY_DIR}/libascendcl_liteos.a
            addlib ${ASCEND_OPENSDK_DIR}/lib/c/ge_exec/libge_executor.a
            addlib ${ASCEND_OPENSDK_DIR}/lib/c/runtime/libruntime.a
            addlib ${ASCEND_OPENSDK_DIR}/lib/libc_base.a
            save
            end")
    endif()

    add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ASCENDCL_C_STATIC_LIB_NAME}
        COMMAND ar -M <${CMAKE_CURRENT_BINARY_DIR}/ascendcl_c_static.mri
        DEPENDS ascendcl_c_static_liteos ge_executor_c_static c_base_static runtime_static ${CMAKE_CURRENT_BINARY_DIR}/ascendcl_c_static.mri
    )

    add_custom_target(ascendcl_c_static DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${ASCENDCL_C_STATIC_LIB_NAME})

    install(
        FILES ${CMAKE_CURRENT_BINARY_DIR}/${ASCENDCL_C_STATIC_LIB_NAME}
        DESTINATION ${INSTALL_LIBRARY_DIR}/c OPTIONAL
    )
    # LiteOS end
endif()
